package cn.edu.lingnan.controller;

import cn.edu.lingnan.pojo.Country;
import cn.edu.lingnan.pojo.Student;
import cn.edu.lingnan.service.CountryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.SQLException;

@Controller
@RequestMapping("/country")
public class CountryController {

    @Autowired
    @Qualifier("countryService")
    private CountryService countryService;

    @RequestMapping("/login")
    public String login(String country_name,String password,Model model){
        Country country = countryService.findCountryByNameAndPassword(country_name, password);
        model.addAttribute("cou",country);
        if(country !=null){
//            return "home";
            return "redirect:/index0.jsp";
        }
        else {
            return "redirect:/index.html";
        }
    }

    @RequestMapping("/insertCountry")
    public String insertCountry(Country country) {
        countryService.insertInfoToCountry(country);
        return "redirect:/index.html";
    }
    @RequestMapping("/selectCountryAll")
    public String findAllCountry(Model model) {
        model.addAttribute("allCou", countryService.findAllCountry());
//        System.out.println(" hhh   "+countryService.findAllCountry().size());
        return "admin/allCountry";//返回到allallCountry这个页面
    }

    @RequestMapping("/toUpdateInfotoCountryByCountry_id")
    public String toUpdateInfotoCountryByCountry_id(int country_id,Model model){
        Country country = countryService.findCountryByCountry_id(country_id);
        model.addAttribute("cou",country);
        return "admin/updateCountry";
    }

@RequestMapping("/updateInfotoCountryByCountry_id")
    public String updateInfotoCountryByCountry_id(Country country){
        countryService.updataInfotoCountry(country);
        return "redirect:/country/selectCountryAll";
    }

    @RequestMapping("/deleteCountryByCountry_id")
    public String deleteCountryByCountry_id(int country_id) throws SQLException {
        if (countryService.findCountryByCountry_idIfInC_V(country_id)==null){
            countryService.deleteCountry(country_id);
        }
        return "redirect:/country/selectCountryAll";
    }





}
